<template>
  <div class="search">
    <Row>
      <Col>
        <Card style="padding: 200px">
          <Row class="operation">
            <Form :label-width="170" ref="form" :model="form" :rules="formValidate">
              <FormItem label="订单冻结天数" prop="freezeOrderTime">
                <InputNumber v-model="form.freezeOrderTime" placeholder="请输入订单冻结天数" type="number" style="width: 100%" :min="0" @on-change="changeNumber($event,1)" ></InputNumber>
              </FormItem>
              <FormItem label="自动收货天数" prop="autoReceiveOrderTime">
                <InputNumber v-model="form.autoReceiveOrderTime" placeholder="请输入自动收货天数" type="number" style="width: 100%" :min="0" @on-change="changeNumber($event,2)" ></InputNumber>
              </FormItem>
              <FormItem label="自动评价天数" prop="autoCommentOrderTime">
                <InputNumber v-model="form.autoCommentOrderTime" placeholder="请输入自动评价天数" type="number" style="width: 100%" :min="0" @on-change="changeNumber($event,3)" ></InputNumber>
              </FormItem>
              <FormItem label="保证金" prop="depositMoney">
                <Input v-model="form.depositMoney" placeholder="请输入保证金" type="text" style="width: 100%" />
              </FormItem>
            </Form>
          </Row>

          <Row style="display: flex;justify-content: space-around;">
            <Button type="default" @click="handleReset">重置</Button>
            <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
          </Row>
        </Card>
      </Col>
    </Row>
    
  </div>
</template>

<script>
import circleLoading from "@/views/my-components/circle-loading.vue";
export default {
  name: "couponAdminManage",
  components: {
    circleLoading,
  },
  data() {
    return {
      loading: true, // 表单加载状态
      modalType: 0, // 添加或编辑标识
      modalVisible: false, // 添加或编辑显示
      modalTitle: "", // 添加或编辑标题
      form: { // 添加或编辑表单对象初始化数据
        id: "",
        freezeOrderTime: 0,
        autoReceiveOrderTime: 0,
        autoCommentOrderTime: 0,
        depositMoney: ""
      },
      // 表单验证规则
      formValidate: {
        freezeOrderTime: [{ required: true, message: "订单冻结天数不能为空", trigger: "blur", type: "number"  }],
        autoReceiveOrderTime: [{ required: true, message: "自动收货天数不能为空", trigger: "blur", type: "number" }],
        autoCommentOrderTime: [{ required: true, message: "自动评价天数不能为空", trigger: "blur", type: "number" }],
        depositMoney: [{ required: true, message: "请输入保证金", trigger: "blur", pattern:/^(([1-9]\d{0,9})|0)(\.\d{0,2})?$/ },
                    { required: true, message: "保证金格式不正确",pattern:/^(([1-9]\d{0,9})|0)(\.\d{0,2})?$/}],
      },
      submitLoading: false, // 添加或编辑提交状态
    };
  },
  methods: {
    init() {
      this.getDataList();
    },
    handleReset() {
      this.$refs.form.resetFields();
    },
    getDataList() {
        this.loading = true;
        // 带多条件搜索参数获取表单数据 请自行修改接口
        this.postRequest("/commonParam/getCommonParamVo").then(res => {
            this.loading = false;
            if (res.success) {
              this.form = res.result;
            } else {
              this.form = {};
            }
        });
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true;
          if (this.modalType === 0) {
            // 添加 避免编辑后传入id等数据 记得删除
            // delete this.form.id;
            this.postRequest("/commonParam/insertOrUpdateCommonParam", this.form).then(res => {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.getDataList();
                this.modalVisible = false;
              } else {
                this.$Message.error(res.message);
                this.modalVisible = false;
              }
            });
          } else {
            this.postRequest("/discountCoupon/insertOrderUpdateDiscountCoupon", this.form).then(res => {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.getDataList();
                this.modalVisible = false;
              }
            });
          }
        }
      });
    },
    changeNumber (event,k) {
      if (event < 0 && k === 1) {
        this.$Message.warning("订单冻结天数不能小于0");
        this.form.freezeOrderTime = 0;
      } else if (event < 0 && k === 2) {
        this.$Message.warning("自动收货天数不能小于0");
        this.form.autoReceiveOrderTime = 0;
      } else if (event < 0 && k === 3) {
        this.$Message.warning("自动评价天数不能小于0");
        this.form.autoCommentOrderTime = 0;
      }
    }
},
  mounted() {
    this.init();
  }
};
</script>
<style lang="less">
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-size: 13px;
        font-weight: 600;  
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        font-size: 13px;
        margin-left: 5px;
    }
}
</style>